#include "httpServer.hpp"
#include "util.hpp"
#include <memory>
#include <vector>

using namespace std;

//web根目录
const string webRoot = "./wwwroot";
const string defaultHomePage = "index.html";
const string page_404 = "err_404.html";

const string SEP = "\r\n";

class httpRequest{
public:
    httpRequest():_path(webRoot){

    }

    ~httpRequest(){

    }

    void Print(){
        logMessage(DEBUG, "method: %s, url: %s, version: %s", _method.c_str(), _url.c_str(), _version.c_str());
        // for(const auto &line : _body){
        //     logMessage(DEBUG, "-%s", line.c_str());
        // }
        logMessage(DEBUG, "path: %s", _path.c_str());
    }

public:
    string _method;
    string _url;
    string _version;
    vector<string> _body;
    string _path;
    string _suffix;
};

httpRequest Deserialize(string &request){
    httpRequest req;
    string line = util::ReadOneLine(request, SEP);
    util::ParseRequestLine(line, &req._method, &req._url, &req._version);

    while (!request.empty()) {
        line = util::ReadOneLine(request, SEP);
        req._body.push_back(line);
    }

    req._path += req._url;
    //如果访问的是一个单/则自动拼接成首页
    if(req._path[req._path.size()-1]=='/'){
        req._path += defaultHomePage;
    }

    //识别路径所指向的文件类型
    auto pos = req._path.rfind(".");
    if(pos=string::npos){
        req._suffix = ".html";
    }else{
        req._suffix = req._path.substr(pos);
    }

    return req;
}

//通过后缀来填充Conten-Type字段
string GetContentType(const string &suffix){
    string content_type = "Content-Type: ";
    if(suffix == ".html" || suffix == ".htm"){ content_type + "text/html"; }
    else if(suffix == ".css"){ content_type += "text/css"; }
    else if(suffix == ".js"){ content_type += "application/x-javascript"; }
    else if(suffix == ".jpg"){ content_type += "image/jpg"; }
    else if(suffix == ".png"){  content_type += "image/png"; }
    else{}

    return content_type;
}

string handlerHttp(string &request){

    //1、读取请求
    //不作如何处理，直接发回请求
    cout << "-------------------------------" << endl;
    cout << request << endl;

    // //2、反序列化和分析请求
    // httpRequest req = Deserialize(request);
    // req.Print();
    // //3、做出响应

    // string body;
    // // 读取指定的html文件，存放到body中
    // //  util::ReadFile(path, &body);

    // bool ret = util::ReadFile(req._path, &body);

    // string response;
    // if (ret == true){
    //     //状态行
    //     response = "HTTP/1.0 200 OK" + SEP;
    //     response += "Content-Length: "+ to_string(body.size()) + SEP;
    //     // response += "Content-Type: text/html" + SEP;
    //     response += GetContentType(req._suffix) + SEP;
    //     response += SEP;

    //     //报头
    //     // response += "hello http";
        
    //     response += body;
    // }else{
    //     //状态行
    //     response = "HTTP/1.0 404 Not Found" + SEP;
    //     util::ReadFile(page_404, &body);
    //     response += "Content-Length: " + to_string(body.size()) + SEP;
    //     response += GetContentType(".html") + SEP;
    //     response += SEP;
    //     response += body;
    // }

    // //4、重定向测试
    // string response;
    // response = "HTTP/1.0 302 Found" + SEP;
    // response += "Location: https://www.qq.com/" + SEP;
    // response += SEP;

    //5、cookie和session
    string response;
    response = "HTTP/1.0 302 Found" + SEP;
    response += "Set-Cookie: sessionid=123" + SEP;
    response += SEP;

    return response;
}

int main(int argc, char* argv[]){

    if(argc != 2){
        exit(USAGE_ERR);
    }

    uint16_t port = atoi(argv[1]);
    unique_ptr<httpServer> tsvr(new httpServer(handlerHttp, port));
    tsvr->InitServer();
    tsvr->Start();
    return 0;
}